$(function(){ 	 	  
	  $('#orders').datagrid({
	      url: 'viewOrder',
	      method:'get',
	      rownumbers:true,
	      pagination:true,
	      pageSize:20,
	      pageList:[10,20,30],
	      fitColumns:true,
	      autoRowHeight:true,
	      singleSelect:true,
	      striped:true,
	      idField:'id',
	      columns:[[
			  {field:'id', title:'Payment Id', hidden:true},
	          {field:'rental.inventory.film.title', title:'Film Title', formatter:function(value, row){
	              return row.rental.inventory.film.title;
	          }},
	          {field:'staff.store.address.address1', title:'Store Address', formatter:function(value, row){
	              return row.rental.inventory.store.address.address1;
	          }},
	          {field:'rental.inventory.film.rentalRate', title:'Rental Rate', formatter:function(value, row){
	        	  return '$' + row.rental.inventory.film.rentalRate;
	          }},
	          {field:'amount', title:'Total Amount', formatter:function(value, row){
	        	  return '$' + row.amount;
	          }},
	          {field:'rental.rentalDate', title:'Rental Date', formatter:function(value, row, index){
	        	  return row.rental.rentalDate;
	          }},
	          {field:'rental.returnDate', title:'Return Date', formatter:function(value, row, index){
	        	  if(!row.rental.returnDate){
	        		  return '<span sytle=\"color:red;\">Not Returned Yet</span>';
	        	  }
	        	  return row.rental.returnDate;
	          }}
	          ]],
          onLoadSuccess:function (data){
        		if(!data.total) {
        			$('#tableTitle').text('You have not any order yet');
        		}else{
        			$('#tableTitle').text('');
        		}
          }
	   });
	  
	  $('#returnOrderTable').click(function(){
		  $('html, body').animate({
		        scrollTop: $('#order').offset().top
		    }, 1500);
	  });
	  
	  $('#viewOwnInfo').click(function(){
		  $(this).parent().parent().hide();
		  $('#hideOwnInfo').parent().parent().show();
		  viewOwnInfo();
		  $('#myInfo').slideDown('slow');
	  });
	  $('#hideOwnInfo').click(function(){
		  $(this).parent().parent().hide();
		  $('#viewOwnInfo').parent().parent().show();
		  $('#myInfo').slideUp('slow');
	  });
}); 

function doSearch(){
	if(!$('#startDate').datebox('getValue') && !$('#endDate').datebox('getValue')){
		 $('#orders').datagrid('load');
	 }else if(!$('#startDate').datebox('getValue')){
		 $('#orders').datagrid('load',{
		        endDate: $('#endDate').datebox('getValue')
		 });
	 }else if(!$('#endDate').datebox('getValue')){
		 $('#orders').datagrid('load',{
		        startDate: $('#startDate').datebox('getValue')
		 });
	 }else{
		 $('#orders').datagrid('load',{
		        startDate: $('#startDate').datebox('getValue'),
		        endDate: $('#endDate').datebox('getValue')
		 });
	 } 
}

function viewOwnInfo(){
	$.get(
	  'viewOwn',
	  function(result){
		  $('#myInfo').children('.row :first').children('div :last').text(result.name.firstName)
							  .parent().next().children('div :last').text(result.name.lastName)
							  .parent().next().children('div :last').text(result.address.phone)
							  .parent().next().children('div :last').text(result.address.address1)
							  .parent().next().children('div :last').text(result.address.district)
							  .parent().next().children('div :last').text(result.address.city.name)
							  .parent().next().children('div :last').text(result.store.address.address1);
	  }
	);
}